package com.logic.landseaserver.task;

import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.logic.landseaserver.common.enums.BillStatusEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.models.prepayment.service.AutoPayBillService;
import com.logic.landseaserver.service.IBillService;
import com.logic.landseaserver.service.IScheduleService;

/**
 * Created by Junny on 2017/7/10.
 */
@Component
public class BillTask {

    private static final Logger log = LoggerFactory.getLogger(BillTask.class);

    @Autowired
    private IBillService billServiceImpl;

    @Autowired
    private IScheduleService iScheduleService;
    
    @Autowired
    private AutoPayBillService autoPayBillService;

   @Scheduled(cron = "0 0 1 1 * ?")
   public void createMonthBill() throws LandseaException {
        // 定时任务每天凌晨1点启动
        // int excuteDay =Integer.parseInt(configurationService.getStringValue(ConfigurationService.BILL_TASK_EXCUTE_DAY));
        // 如果不是指定的执行日期，则退出
        /*if (!DateUtil.checkIsDayOfMonth(excuteDay))
        {
            log.info("today is not " + excuteDay + ",createMonthBill task can not run.");
            return;
        }*/
        //月账单
        boolean cuteFlag = iScheduleService.cuteSchedule();
        if (cuteFlag) {
            log.info("Task for createMonthBill start");
            billServiceImpl.createMonthBill();
            log.info("Task for createMonthBill end");
        }
    }



    @Scheduled(cron = "0 0 1 * * ?")
    public void createLateFee() throws LandseaException {
        // 定时任务每天凌晨1点启动
        boolean cuteFlag = iScheduleService.cuteSchedule();
        if (cuteFlag) {
            log.info("Task for createLateFee start");
            billServiceImpl.createLateFee();
            log.info("Task for createLateFee end");
        }
    }

    //退房账单超过N天不处理将发送短信给管家 // 定时任务每天9点启动
    @Scheduled(cron = "0 0 9 * * ?")
    public void delayOffBillSendMsg() throws LandseaException {
        boolean cuteFlag = iScheduleService.sendMsgFlag();
        if (cuteFlag) {
            log.info("Task for delayOffBillSendMsg start");
            billServiceImpl.delayOffBillSendMsg();
            log.info("Task for delayOffBillSendMsg end");
        }
    }


    /**
     * 每月一号8点发送期账单推送消息
     * @throws LandseaException
     */
    @Scheduled(cron = "0 0 8 1 * ?")
    public void sendMonthBillMsg() throws LandseaException {
        //月账单生成消息推送
        boolean cuteFlag = iScheduleService.sendMsgFlag();
        if (cuteFlag) {
            log.info("Task for sendMonthBillMsg start");
            HashMap msgMap = new HashMap();
            msgMap.put("sendDate", DateUtil.getInstence().convertDate(new Date()));
            msgMap.put("billName", "第"); //月结账单
            msgMap.put("billStatus", BillStatusEnum.BIST3.getCode());
            billServiceImpl.sendMsgByMonthBill(msgMap);
            log.info("Task for sendMonthBillMsg end");
            
            // 处理首期账单自动抵扣的余额变动消息
            autoPayBillService.sendDbWechatMsg();
        }
    }
}
